package com.yilifang.service.impl;

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;
import com.yilifang.dao.master.MasterDirectSqlMapper;
import com.yilifang.service.ZbdlApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhuan on 2018-09-14.
 */
@Slf4j
@Service
public class ZbdlApiServiceImpl implements ZbdlApiService {

    //主数据库
    @Autowired
    MasterDirectSqlMapper masterDirectSqlMapper;

    //redis操作
    @Autowired
    private RedisTemplate redisTemplate;
    //redis操作
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 招标代理公告和内容
     * @param page
     * @param size
     * @return
     */
    public String getZbdlAnnounce(Integer page,Integer size){

        //缓存
        String key = "ZBDL" + page + size;
        String zbdlAnnounceJson = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(zbdlAnnounceJson)){
            List<Map<String, Object>> zbdlAnnounce = masterDirectSqlMapper.selectZbdlAnnounce(page, size);
            zbdlAnnounceJson = JSON.toJSONString(zbdlAnnounce);
            stringRedisTemplate.opsForValue().set(key,zbdlAnnounceJson,20, TimeUnit.MINUTES);
        }
        //无缓存
//        List<Map<String, Object>> zbdlAnnounce = masterDirectSqlMapper.selectZbdlAnnounce(page, size);
//        String zbdlAnnounceJson =  JSON.toJSONString(zbdlAnnounce);
        return  zbdlAnnounceJson;
    }
    /**
     * 招标代理公告列表
     * @param page
     * @param size
     * @return
     */
    public Object getZbdlAnnounceList(Integer page,Integer size,String vannouncetype){

//        缓存
        String key = "ZBDL-List" + page + size +vannouncetype;
        Object zbdlAnnounceListJson = redisTemplate.opsForValue().get(key);
        if (StringUtils.isEmpty(zbdlAnnounceListJson)){

            List<Map<String, Object>> zbdlAnnounceList = masterDirectSqlMapper.selectZbdlAnnounceList(page, size ,vannouncetype);
            Map<String, Object> zbdlAnnounceCount = masterDirectSqlMapper.selectZbdlAnnounceCount(vannouncetype);

            Map<String,Object> zbdlAnnounceListAndCount = new HashMap<>();
            zbdlAnnounceListAndCount.put("zbdlAnnounceCount",zbdlAnnounceCount.get("ZBDLANNOUNCECOUNT"));
            zbdlAnnounceListAndCount.put("zbdlAnnounceList",zbdlAnnounceList);
            zbdlAnnounceListJson = JSONObject.toJSON(zbdlAnnounceListAndCount);
            redisTemplate.opsForValue().set(key,zbdlAnnounceListJson,20, TimeUnit.MINUTES);
        }
        log.info(zbdlAnnounceListJson.toString());
        return zbdlAnnounceListJson;
    }
    /**
     * 招标代理公告Html
     * @param id
     * @return
     */
    public Object getZbdlAnnounceHtml(String id){
        Map<String, Object> zbdlAnnounceHtmlMap = masterDirectSqlMapper.selectZbdlAnnounceHtmlById(id);
        return JSON.toJSON(zbdlAnnounceHtmlMap) ;
    }

}
